<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
	unset($_SESSION['searchstring']);
	unset($_SESSION['sortstring']);
	$rows_per_page = 30;
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
	
$order_by = " order by SUBSTRING_INDEX(e.FullName, ' ', -1) ASC ";
$_SESSION['sortstring'] = $order_by;
	
$sqlempbreak = mysql_query("SELECT e.IDEmployee,e.FullName,DATE_FORMAT(e.Birthday,'%d/%m/%Y') as Birthday,e.UserName,al.SexName,dl.DepartmentName,e.Position FROM employee e ,department_lang dl, ac_sex_lang al, roles rl 
						   where e.IDDepartment = dl.IDDepartment and dl.LangID = '".$_SESSION['lang']."' 
						   and e.Sex = al.SexID and al.LangID = '".$_SESSION['lang']."'  
						   and e.IDRole= rl.IDRole and e.IDRole != 1 and e.State = 0 ".$order_by." limit ".$page_start.",".$rows_per_page);

$sqlempbreakPage = mysql_query("SELECT e.IDEmployee,e.FullName,DATE_FORMAT(e.Birthday,'%d/%m/%Y') as Birthday,e.UserName,al.SexName,dl.DepartmentName,e.Position FROM employee e ,department_lang dl, ac_sex_lang al, roles rl 
						   where e.IDDepartment = dl.IDDepartment and dl.LangID = '".$_SESSION['lang']."' 
						   and e.Sex = al.SexID and al.LangID = '".$_SESSION['lang']."'  
						   and e.IDRole= rl.IDRole and e.IDRole != 1 and e.State = 0 ".$order_by);


$number_of_page = ceil ( mysql_num_rows( $sqlempbreakPage ) / $rows_per_page );

?>
<script type="text/javascript">
function sortFullname(){
	var orderby = $('input#sortfullname').val();	
	var l_edit = $('input#l_edit').val();
	var l_delete = $('input#l_delete').val();
	$.ajax({url:"admin/employeemanagement/ajaxsortboke.php", data : {orderby : orderby , flags : 'fullname', l_edit : l_edit, l_delete : l_delete}, success:function(result){
		$("table#listEmp tr:not(:first)").remove();
		$("table#listEmp thead").append(result);
		if (orderby==<?=SORT_ASC?>){
			$('input#sortfullname').val(<?=SORT_DESC?>);
		}else{
			$('input#sortfullname').val(<?=SORT_ASC?>);
		}
	}});
}

function sortUsername(){
	var orderby = $('input#sortusername').val();	
	var l_edit = $('input#l_edit').val();
	var l_delete = $('input#l_delete').val();
	$.ajax({url:"admin/employeemanagement/ajaxsortboke.php", data : {orderby : orderby , flags : 'username', l_edit : l_edit, l_delete : l_delete}, success:function(result){
		$("table#listEmp tr:not(:first)").remove();
		$("table#listEmp thead").append(result);
		if (orderby==<?=SORT_ASC?>){
			$('input#sortusername').val(<?=SORT_DESC?>);
		}else{
			$('input#sortusername').val(<?=SORT_ASC?>);
		}
	}});
}
function paging(page, search_str){
	var l_edit = $('input#l_edit').val();
	var l_delete = $('input#l_delete').val();
	$.ajax({url:"admin/employeemanagement/ajaxpagingsortbroke.php", data : {pages : page, l_edit : l_edit, l_delete : l_delete}, success:function(result){
		$("table#listEmp tr:not(:first)").remove();
		$("table#listEmp thead").append(result);
	}});
}

function searching(page, eleID){ 
	var l_edit = $('input#l_edit').val();
	var l_delete = $('input#l_delete').val();
	var typeSearch = $('select#typeSearch').val();
	var search_s = $('input#fullname').val();
	$.ajax({url:"admin/employeemanagement/ajaxsearchemployeeboke.php", data : {pages : page, typeSearch : typeSearch , search_s : search_s ,  l_edit : l_edit, l_delete : l_delete}, success:function(result){
		$("table#listEmp tr:not(:first)").remove();
		$("table#listEmp thead").append(result);
	}});
}

function searchingAuto(page, eleID){ 
	var l_edit = $('input#l_edit').val();
	var l_delete = $('input#l_delete').val();
	if($('input#flags').val()==''){
		$('input#flags').val('processing');
		var typeSearch = $('select#typeSearch').val();
		var search_s = $('input#fullname').val();
		$.ajax({url:"admin/employeemanagement/ajaxsearchemployeeboke.php", data : {pages : page, typeSearch : typeSearch , search_s : search_s , l_edit : l_edit, l_delete : l_delete }, success:function(result){
			$("table#listEmp tr:not(:first)").remove();
			$("table#listEmp thead").append(result);
			if($('input#flags').val()=='waiting'){
				$('input#flags').val('');
				searchingAuto(1,'');								
			}
			else{
				$('input#flags').val('');
			}
		}});
	}else{
		$('input#flags').val('waiting');
	}
}

function searchingAll(page, search_str){
	var l_edit = $('input#l_edit').val();
	var l_delete = $('input#l_delete').val();
	$('input#fullname').val('');
	$.ajax({url:"admin/employeemanagement/ajaxpagingemployeeboke.php", data : {pages : page, searchAll : search_str,  l_edit : l_edit, l_delete : l_delete}, success:function(result){
		$("table#listEmp tr:not(:first)").remove();
		$("table#listEmp thead").append(result);
	}});
}

function onclickDelete(idemployee,els){
	$.ajax({url:"admin/employeemanagement/process-delete-employee-broke.php", data : {idemployee : idemployee,  l_edit : l_edit, l_delete : l_delete}, success : function(result){
		$("table#listEmp tr:not(:first)").remove();
		$("table#listEmp thead").append(result);
	}});
	
}
</script>

<div id="contains" style="background-color: #edf4f7;">
	<div class="title">
		<div
			style="color: #0962C8; font-size: 12px; font-weight: bold; padding-left: 2px; padding-top: 8px; text-transform: uppercase;"></div>
	</div>
	<div class="body" style="margin-left: 5px;">

		<div style="float: left; width: 150px; margin: 0px 0px 5px 0px;">
			<a href="create-new-account-admin.php"><input id="createaccount" name="createaccount" value="<?=$l_createaccount?>"
				class="buttonNormal" style="width: 140px" type="submit" /></a>
			<input type="hidden" value="<?=$l_edit?>" id="l_edit"/>	
			<input type="hidden" value="<?=$l_delete?>" id="l_delete"/>	
		</div>
		<div
			style="float: right; width: 520px; text-align: right; margin: 0px 5px 5px 0px;">
			<select id="typeSearch" name="typeSearch" size="1"
				style="width: 120px;height: 21px;">
				<option value="fullname"><?=$l_fullname?></option>
				<option value="username"><?=$l_username?></option>					
			</select> <i></i>
			<input type="hidden" id="flags"/>
			<input id="fullname" name="fullname" type="text" onkeyup="searchingAuto(1,'')"
				value="" style="width: 177px;" class="textfiledCompany" /> <i></i><input
				id="search" name="search" type="button" value="<?=$l_search?>" onclick="searching(1,'')"
				class="buttonNormal" /> <i></i><input
				id="all" name="all" value="<?=$l_all?>" onclick="searchingAll(1,'all')"
				class="buttonNormal"
				type="button" />
		</div>
		<br style="clear: both" />
		<table id="listEmp" class="rf-dt" style="width: 100%;">
			<thead class="rf-dt-thd" >
				<tr class="rf-dt-shdr" >
					<th width="7%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col" id="no"><?=$l_no?></th>
					<th width="22%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col">
					<a href="#" onclick="sortFullname()"> <?=$l_employee_name?></a>
		                <input type="hidden" id="sortfullname" value="<?=SORT_ASC?>"/>
		            </th>
					<th width="10%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col" id="birthday"><?=$l_birthday?></th>
					<th width="15%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col">
					<a href="#" onclick="sortUsername()"> <?=$l_username?></a>
		                <input type="hidden" id="sortusername" value="<?=SORT_ASC?>"/>
		            </th>
					<th width="8%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><?=$l_sex?></th>
					<th width="10%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><?=$l_department?></th>
					<th width="10%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"><?=$l_position?></th>
					<th width="9%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"></th>
					<th width="9%" class="rf-dt-shdr-c" style="background-color: #eeeeee;" scope="col"></th>
				</tr>
				<?php $i =0;
				 while ($row=mysql_fetch_array($sqlempbreak)){ 
				 	$i++ ;?>
					<tr>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $i;?></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: left: ;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-broke.php&id=<?=$row[IDEmployee]?>"><?php echo $row[FullName];?></a></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $row[Birthday];?></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-broke.php&id=<?=$row[IDEmployee]?>"><?php echo $row[UserName]?></a></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $row[SexName]?></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $row[DepartmentName]?></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><?php echo $row[Position]?></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col"><a href="employeemanager.php?content=admin/employeemanagement/content-edit-broke.php&id=<?=$row[IDEmployee]?>"><?=$l_edit?></a></td>
					<td class="rf-dt-c" style="font-size: 12px;text-align: center;" scope="col">
					<a href="#" onclick="if(confirm('<?php echo $l_confirm_deleteproject?>')){  onclickDelete('<?=$row[IDEmployee]?>',this); return true;}else{return false;}"><?=$l_delete?></a></td>
					</tr>
				<?php }?>
				<tr class="trfoot">
	                    	<td class="rf-dt-ftr-c" scope="colgroup" colspan="9">
	                        	<div style="text-align: center; width: 100%">
	                            	<span class="rf-ds">
															
															<?php if ( $number_of_page > 1 ) 
															{ 
																if ($page==1)
																	$p=2;
																else if ($page==$number_of_page)
																	$p=$number_of_page-1;
																else $p=$page;
																if($page!=1){?>
																	<a onclick="paging(1,'')" ><span class='button_next'><?php echo "<<<";?></span> </a>
																	<a onclick="paging(<?=($page-1)?>,'')" ><span class='button_next'><?php echo "<<";?></span> </a>
																<?php  }
																else{
																	echo "<span class='button_next'><<<</span>"; 
																	echo "<span class='button_next'><<</span>";
																}
																for ( $i = $p-1; $i <= $p+1; $i++ ) {	
																		if ($i<=$number_of_page && $i>0)
																		
																			if ($i==$page){
																				echo "<span class='button_next' style='color: #000'><i>".$i."</i></span>"; 
																			}else{
																			?>
																				<a onclick="paging(<?=$i?>,'')" ><span class="button_next">
																				<?php 
																				echo $i;
																				?>
																				</span> </a>
																			<?php 
																			}
																		?>
																	
																<?php 
																		}
																	
																	if($page!=$number_of_page){ ?>
																		<a onclick="paging(<?=($page+1)?>,'')"><span class='button_next'><?php echo ">>";?></span> </a>
																		<a onclick="paging(<?=($number_of_page)?>,'')" ><span class='button_next'><?php echo ">>>";?></span> </a>
																	<?php }
																	else{
																		echo "<span class='button_next'>>></span> ";
																		echo "<span class='button_next'>>>></span> ";
																	}
																}
															
																
																?>
																													
														</span>
														</div>
														</td>
	                    </tr>
			</thead>
		</table>


	</div>
</div>
